﻿using AutoMapper;
using DFGK.IRepository;
using DFGK.IService;
using DFGK_WMS.Model;
using DFGK_WMS.Model.Dto;
using DFGK_WMS.Model.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DFGK.Service
{
    public class SupplierService : BaseService, ISupplierService
    {
        private readonly ISupplierRepository _supplierRepository;
        private readonly IDictItemRepository _itemRepository;
        private readonly IUnitOfWork _unitOfWork;
        public SupplierService(IMapper mapper, IUnitOfWork unitOfWork, ISupplierRepository _supplierRepository, IDictItemRepository itemRepository) : base(mapper)
        {
            this._supplierRepository = _supplierRepository;
            _unitOfWork = unitOfWork;
            _itemRepository = itemRepository;
        }

        public bool AddSupplier(Supplier dictItem, out string message)
        {
            message = "";
            var sobj = _supplierRepository.QueryEntityWhere(x => x.Code == dictItem.Code);
            if (sobj != null)
            {
                message = "编码已存在！";
                return false;
            }
            if (dictItem.TypeId == null)
            {
                message = "未选择类型！";
                return false;
            }
            //供应商类型
            var dicitemobj = _itemRepository.QueryEntityById((long)dictItem.TypeId);
            if (dicitemobj != null)
            {
                dictItem.TypeName = dicitemobj.ItemName;
            }
            _supplierRepository.AddEntity(dictItem);
            return _unitOfWork.SaveChangesEntity();
        }

        public bool DelSupplier(long id)
        {
            var sup = _supplierRepository.QueryEntityById(id);
            if (sup != null)
            {
                _supplierRepository.DelEntity(sup);
                return _unitOfWork.SaveChangesEntity();
            }
            return false;
        }

        public List<Supplier> GetAllSupplier(string? name, int pageIndex, int pageSize, out int totalCount)
        {
            var supList = _supplierRepository.GetEntityAlls();
            if (!string.IsNullOrEmpty(name))
            {
                supList = supList.Where(c => c.Name.Contains(name));
            }
            var list = _supplierRepository.GetEntityAlls(supList, pageIndex, pageSize, out totalCount, x => x.CreateDate, false);
            return list.ToList();
        }

        public Supplier? GetSupplierById(long id)
        {
            return _supplierRepository.QueryEntityById(id);
        }

        public bool AddSuppliers(List<Supplier> suppliers)
        {
            int count = 0;
            foreach (var item in suppliers)
            {
                _supplierRepository.AddEntity(item);
                _unitOfWork.SaveChangesEntity();
                count++;
            }
            if (count <= 0)
            {
                return false;
            }
            return true;
        }

        public List<SuppliersDown> GetAllSupplier()
        {
            List<SuppliersDown> suppliers = new List<SuppliersDown>();
            var list = _supplierRepository.GetAllEntityList().ToList();
            foreach (var item in list)
            {
                SuppliersDown down = new SuppliersDown();
                down.Code = item.Code;
                down.Name = item.Name;
                down.TypeName = item.TypeName;
                down.AddrDetail = item.AddrDetail;
                down.Contact = item.Contact;
                down.ContactMobile = item.ContactMobile;
                down.Email = item.Email;
                down.Remark = item.Remark;
                suppliers.Add(down);
            }
            return suppliers;
        }

        public bool UpdSupplier(Supplier supplier, out string message)
        {
            message = "";
            var sobj = _supplierRepository.QueryIsEntityWhere(x => x.Id != supplier.Id && x.Code == supplier.Code);
            if (sobj != null)
            {
                message = "编码已存在！";
                return false;
            }
            if (supplier.TypeId == null)
            {
                message = "未选择类型！";
                return false;
            }
            //供应商类型
            var dicitemobj = _itemRepository.QueryEntityById((long)supplier.TypeId);
            if (dicitemobj != null)
            {
                supplier.TypeName = dicitemobj.ItemName;
            }
            _supplierRepository.UpdEntity(supplier);
            return _unitOfWork.SaveChangesEntity();
        }

        public List<Supplier> GetSupplierMsg(string? purMsg, string? suppliers, int pageIndex, int pageSize, out int totalCount)
        {
            var supList = _supplierRepository.GetEntityAlls();
            if (!string.IsNullOrEmpty(purMsg))
            {
                supList = supList.Where(c => c.Contact1Mobile.Contains(purMsg));
            }
            if (!string.IsNullOrEmpty(suppliers))
            {
                supList = supList.Where(c => c.Name.Contains(suppliers));
            }
            var list = _supplierRepository.GetEntityAlls(supList, pageIndex, pageSize, out totalCount, x => x.CreateDate, false);
            return list.ToList();
        }
    }
}
